//
//  JDChangeNicknameViewController.swift
//  DragonWallet
//
//  Created by volantPig on 2024/5/15.
//

import UIKit
/// 修改用户名
class JDChangeNicknameViewController: JDBaseViewController {

    var changeNicknameCompleted: ((String)->Void)?
    var bottomContainerView: UIView?
    var cancelButton: UIButton?
    var confirmButton: UIButton?
    
    var nicknameInputView: UITextField?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupConfig()
        self.setupUIElements()
        // Do any additional setup after loading the view.
    }
    
    func setupConfig() {
        self.setupNavigationTitle("用户名")
        self.setupLeftBackItem()
    }

    func setupUIElements() {

        self.bottomContainerView = UIView()
        self.view.addSubview(self.bottomContainerView!)
        self.bottomContainerView?.snp.makeConstraints({ make in
            make.left.right.equalTo(self.view)
            make.bottom.equalTo(self.view)
            make.height.equalTo(104)
        })
        self.bottomContainerView?.backgroundColor = .white

        self.cancelButton = UIButton(type: .custom)
        self.bottomContainerView?.addSubview(self.cancelButton!)
        self.cancelButton?.snp.makeConstraints({ make in
            make.right.equalTo(self.bottomContainerView!.snp.centerX).offset(-5)
            make.centerY.equalTo(self.bottomContainerView!)
            make.left.equalTo(self.bottomContainerView!).offset(12)
            make.height.equalTo(54)
        })
        self.cancelButton?.layer.cornerRadius = 27
        self.cancelButton?.backgroundColor = color("#F5F6F8")
        self.cancelButton?.setTitle("返回", for: .normal)
        self.cancelButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.cancelButton?.setTitleColor(.black, for: .normal)
        self.cancelButton?.addTarget(self, action: #selector(backAction), for: .touchUpInside)
        
        self.confirmButton = UIButton(type: .custom)
        self.bottomContainerView?.addSubview(self.confirmButton!)
        self.confirmButton?.snp.makeConstraints({ make in
            make.left.equalTo(self.bottomContainerView!.snp.centerX).offset(5)
            make.right.equalTo(self.bottomContainerView!).offset(-12)
            make.height.equalTo(54)
            make.centerY.equalTo(self.bottomContainerView!)
        })
        self.confirmButton?.setTitle("确定", for: .normal)
        self.confirmButton?.backgroundColor = color("#FA6161")
        self.confirmButton?.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .regular)
        self.confirmButton?.setTitleColor(.white, for: .normal)
        self.confirmButton?.layer.cornerRadius = 27
        self.confirmButton?.addTarget(self, action: #selector(changeNicknameAction), for: .touchUpInside)
        
        self.nicknameInputView = UITextField()
        self.view.addSubview(self.nicknameInputView!)
        self.nicknameInputView?.snp.makeConstraints({ make in
            make.left.equalTo(self.view).offset(12)
            make.right.equalTo(self.view).offset(-12)
            make.top.equalTo(self.view).offset(12+statusBarHeight+44)
            make.height.equalTo(58)
        })
        self.nicknameInputView?.placeholder = "请输入用户名"
        self.nicknameInputView?.textColor = color("#333333")
        self.nicknameInputView?.font = UIFont.systemFont(ofSize: 15, weight: .regular)
        self.nicknameInputView?.backgroundColor = .white
        self.nicknameInputView?.layer.cornerRadius = 9
        self.nicknameInputView?.clearButtonMode = .always
        
        let leftView = JDPaddingLabel(frame: CGRect(x: 0, y: 0, width: 77, height: 58))
        leftView.edgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 20)
        leftView.text = "用户名"
        leftView.labelConfig(textAlignment: .center, font: UIFont.systemFont(ofSize: 15, weight: .medium), textColor: color("#333333"), numberOfLines: 1)
        self.nicknameInputView?.leftView = leftView
        self.nicknameInputView?.leftViewMode = .always
        
        
        if (JDLoginModel.default.nickname?.count ?? 0) > 0 {
            self.nicknameInputView?.text = JDLoginModel.default.nickname
        }
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    @objc func changeNicknameAction() {
        
        var params = [String: Any]()
        params["nickname"] = self.nicknameInputView?.text
        
        JDNetWorkRequest(.changeNickname(params)) { responseStr in
            
            let commonModel = JDCommonModel.deserialize(from: responseStr, designatedPath: "")
            if commonModel?.code == "0" {
                printLog("修改成功")
                JDLoginModel.default.nickname = self.nicknameInputView?.text
                self.backAction()
                self.changeNicknameCompleted?(self.nicknameInputView?.text ?? "")
                    
                let commonModel = JDCommonModel()
                commonModel.msg = "ok"
                commonModel.data = JDLoginModel.default
                commonModel.code = "0"
                
                UserDefaults.standard.set(commonModel.toJSONString(), forKey: "loginInfo")

            } else {
                printLog("修改失败")
            }
            
        } failure: { error in
            
        } error: { errorCode in
            
        }
        
//        printLog("修改用户昵称")
//        DispatchQueue.main.asyncAfter(deadline: .now()+1) {
//            self.backAction()
//
//            if true {
//                // 接口请求成功，修改本地数据
//                JDLoginModel.default.nickname = self.nicknameInputView?.text
//            }
//            self.changeNicknameCompleted?(self.nicknameInputView?.text ?? "")
//        }
    }
    
    
}
